package com.byq.mm.smartlib.cluster.activity.client;




import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.byq.mm.smartlib.R;
import com.byq.mm.abstractx.cluster.client.event.ClusterChangeEvent;
import com.byq.mm.application.cluster.client.PlayerClient;
import com.byq.mm.application.cluster.provider.PlayerProvider;
import com.byq.mm.foundation.cluster.client.event.AttributeEvent;
import com.byq.mm.foundation.cluster.datatype.AttributeValue;
import com.byq.mm.foundation.cluster.datatype.StringListValue;


public class ClientPlayerActivity extends ClientClusterActivity {
	ImageButton previous;
	ImageButton play;
	ImageButton pause;
	ImageButton next;
	SeekBar progress;
	SeekBar volume;
	ImageView mute;
	TextView zero;
	TextView all;
	ListView list;
	TextView current;
	View activatorContainer;
	View playerContainer;
	Button activator;
	PlayerClient pc;
	PlaylistAdapter adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cluster_player);
		pc = (PlayerClient)client;
		
		activatorContainer = this.findViewById(R.id.activatorContainer);
		playerContainer = this.findViewById(R.id.playerContainer);
		activator = (Button)this.findViewById(R.id.activator);
		activator.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pc.toggle();
				
			}
		});
		activator.setEnabled(false);
		previous = (ImageButton)this.findViewById(R.id.previous);
		previous.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pc.select((pc.getCurrentIndex() - 1 + pc.getList().size()) % pc.getList().size());
				
			}
		});
		play = (ImageButton)this.findViewById(R.id.play);
		play.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pc.play();
				
			}
		});
		pause = (ImageButton)this.findViewById(R.id.pause);
		pause.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pc.pause();
				
			}
		});
		next = (ImageButton)this.findViewById(R.id.next);
		next.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pc.select((pc.getCurrentIndex() + 1) % pc.getList().size());
				
			}
		});
		progress = (SeekBar)this.findViewById(R.id.progress);
		
		volume = (SeekBar)this.findViewById(R.id.volume);
		volume.setMax(255);

		volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				pc.volume(seekBar.getProgress() );
				
			}});
		
		progress.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				pc.progress(seekBar.getProgress()* 1000);
				
			}});
		mute = (ImageView)this.findViewById(R.id.mute);
		
		mute.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				pc.toggleMute();
				
			}

			});
		all = (TextView)this.findViewById(R.id.all);
		zero = (TextView)this.findViewById(R.id.zero);
		current = (TextView)this.findViewById(R.id.current);
		list = (ListView)this.findViewById(R.id.list);
		

	 adapter = new PlaylistAdapter();
		list.setAdapter(adapter);
		
		list.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> av, View v, int arg2,
					long arg3) {
				pc.select(arg2);
				
			}
			   
	    });
		pc.refresh();
	}
	
	@Override
	protected void update(ClusterChangeEvent e) {
		if(e instanceof AttributeEvent){
			AttributeEvent ae = (AttributeEvent)e;
			String name = ae.getAttributeName();
			PlayerClient pc = (PlayerClient)client;
			AttributeValue v = pc.getAttributeValue(name);
			if(name.equals(PlayerProvider.Define.Attributes.PLAYLIST)){
				StringListValue sv = (StringListValue)v;
				adapter.notifyDataSetChanged();
				
			}
			if(name.equals(PlayerProvider.Define.Attributes.STATUS)){
				int sv = pc.getStatus();
				if(sv == 0 || sv == 3){
					pause.setVisibility(View.GONE);
					play.setVisibility(View.VISIBLE);
				}
				if(sv == 1){
					pause.setVisibility(View.VISIBLE);
					play.setVisibility(View.GONE);
	
				}
			}
			if(name.equals(PlayerProvider.Define.Attributes.VOLUME)){
				int p = pc.getVolume();
				volume.setProgress(p);
				if(p == 0)
					mute.setImageResource(R.drawable.mute);
				else
					mute.setImageResource(R.drawable.mute_on);
			}
			if(name.equals(PlayerProvider.Define.Attributes.CURRENT_INDEX)){
				int i = pc.getCurrentIndex();
				if(i < 0 || i >= pc.getList().size())
					return;
//				list.setSelection(i);
				current.setText(pc.getList().get(i));
				adapter.notifyDataSetChanged();
				
				
			}
			if(name.equals(PlayerProvider.Define.Attributes.PROGRESS)){
				int p = pc.getProgress()/1000;
				int minute = p/60;
				int seconds = p % 60;
				zero.setText(minute + ":" + seconds);
				progress.setProgress(p);
			}
			if(name.equals(PlayerProvider.Define.Attributes.CURRENT_LENGTH)){
				int maxs = pc.getCurrentLength();
				int minute = maxs/60;
				int seconds = maxs % 60;
				all.setText(minute + ":" + seconds);
				progress.setMax(maxs);
				progress.setProgress(pc.getProgress()/1000);
				
				
			}
			if(name.equals(PlayerProvider.Define.Attributes.ACTIVE)){
				if(pc.isActive()){
					this.playerContainer.setVisibility(View.VISIBLE);
					this.activatorContainer.setVisibility(View.GONE);
					activator.setBackgroundResource(R.drawable.activator_off);
				}else{
					this.playerContainer.setVisibility(View.GONE);
					this.activatorContainer.setVisibility(View.VISIBLE);
					activator.setBackgroundResource(R.drawable.activator_on);
				}
				activator.setEnabled(true);
				
			}
		}
		
	}
class PlaylistAdapter extends BaseAdapter{
	private LayoutInflater inflator =  LayoutInflater.from(ClientPlayerActivity.this);
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pc.getList().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return pc.getList().get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View cv, ViewGroup parent) {
		String item = pc.getList().get(position);
		TextView name = null;

			cv = inflator.inflate(R.layout.list_item_text, null);
			name = (TextView)cv.findViewById(R.id.text);
			name.setText(item);
			
		
		if(pc.getCurrentIndex() == position){
			//cv.setBackgroundColor(R.color.selected_background);
			name.setTextColor(R.color.selected_text);
		}
		return cv;
	}
		
	}

}